/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package resource;

/**
 *
 * @author YAN
 */

import java.sql.*;
import java.util.*;
import util.DBConnection;
public class resource {
	private DBConnection DB;
    private String cr_id;
	private String c_id;
	private String upload_time;
	private String filename;
	private String filetype;
	public resource(){
		DB=new DBConnection();
		DB.connect();
	}
        public void setCr_id(String cr_id){
		this.cr_id=cr_id;
	}
	public String getCr_id(){
		return cr_id;
	}
	public void setC_id(String c_id){
		this.c_id=c_id;
	}
	public String getC_id(){
		return c_id;
	}
	public void setFilename(String filename){
		this.filename=filename;
	}
	public String getFilename(){
		return filename;
	}
	public void setFiletype(String filetype){
		this.filetype=filetype;
	}
	public String getFiletype(){
		return filetype;
	}
	public void setUpload_time(String upload_time){
		this.upload_time=upload_time;
	}
	public String getUpload_time(){
		return upload_time;
	}
	public ArrayList<resource> findByFilename(String filename){
		ArrayList<resource> resource=new ArrayList<resource>();
		String sql="select * from courseresource  where filename like '"+filename+"'";
		ResultSet rs=DB.executeQuery(sql);
		try{
			while(rs.next()){
				resource c=new resource();
                                c.setCr_id(rs.getString("cr_id"));
				c.setC_id(rs.getString("c_id"));
				c.setFilename(rs.getString("filename"));
				c.setFiletype(rs.getString("filetype"));
				c.setUpload_time(rs.getString("upload_time"));
				resource.add(c);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DB.close();
		}
		return resource;
	}
	public ArrayList<resource> findByFiletype(String filetype){
		ArrayList<resource> resource=new ArrayList<resource>();
		String sql="select * from courseresource where filetype like '"+filetype+"'";
		ResultSet rs=DB.executeQuery(sql);
		try{
			while(rs.next()){
				resource c=new resource();
                                c.setCr_id(rs.getString("cr_id"));
				c.setC_id(rs.getString("c_id"));
				c.setFilename(rs.getString("filename"));
				c.setFiletype(rs.getString("filetype"));
				c.setUpload_time(rs.getString("upload_time"));
				resource.add(c);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DB.close();
		}
		return resource;
	}
	public ArrayList<resource> findByC_id(String c_id){
		ArrayList<resource> resource=new ArrayList<resource>();
		String sql="select * from courseresource where c_id like '"+c_id+"'";
		ResultSet rs=DB.executeQuery(sql);
		try{
			while(rs.next()){
				resource c=new resource();
                                c.setCr_id(rs.getString("cr_id"));
				c.setC_id(rs.getString("c_id"));
				c.setFilename(rs.getString("filename"));
				c.setFiletype(rs.getString("filetype"));
				c.setUpload_time(rs.getString("upload_time"));
				resource.add(c);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DB.close();
		}
		return resource;
	}
	public ArrayList<resource> findByUpload_time(String upload_time){
		ArrayList<resource> resource=new ArrayList<resource>();
		String sql="select * from courseresource where upload_time like '"+upload_time+"'";
		ResultSet rs=DB.executeQuery(sql);
		resource c=new resource();
		try{
			while(rs.next()){
               c.setCr_id(rs.getString("cr_id"));
				c.setC_id(rs.getString("c_id"));
				c.setFilename(rs.getString("filename"));
				c.setFiletype(rs.getString("filetype"));
				c.setUpload_time(rs.getString("upload_time"));
				resource.add(c);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DB.close();
		}
		return resource;
	}
	/*public boolean save(){
		String sql="insert into resource(c_id,cname,t_id,category,teach_outline,teach_plan)" +
				"values(null, ?, ?, ?, ?)";
		PreparedStatement pstmt = DB.prepare(sql);
		try {
			pstmt.setString(1, cname);
			pstmt.setString(2, category);
			pstmt.setString(3, teach_outline);
			pstmt.setString(4, teach_plan);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DB.close(pstmt);
			DB.close();
		}
	}*/

}
